package oocl.ita.eordering.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import oocl.ita.eordering.po.Client;
import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.service.ClientService;
import oocl.ita.eordering.service.MerchantService;
import oocl.ita.eordering.service.OrderService;
import oocl.ita.eordering.util.BaseUtil;
import oocl.ita.eordering.vo.Result;
import oocl.ita.eordering.vo.Vorder;

@Controller
@RequestMapping("/action")
public class ClientOperationController {

	@Autowired(required = true)
	private OrderService orderService;
	@Autowired(required = true)
	private ClientService clientService;
	@Autowired(required = true)
	private MerchantService merchantService;

	// 修改个人信息
	@RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
	@ResponseBody
	public Result updatePassword(String oldPwd, String newPwd, HttpSession session) {
		Result result = new Result();
		Client client = (Client) session.getAttribute("c");
		if (client == null) {
			result.fail("请登录");
			return result;
		}
		String oldPwd_after = BaseUtil.JohnMd(oldPwd, "md5");
		if (!oldPwd_after.equals(client.getcPassword())) {
			result.fail("原密码错误");
			return result;
		} else {
			String newPwd_after = BaseUtil.JohnMd(newPwd, "md5");
			client.setcPassword(newPwd_after);
			clientService.updateClientInfo(client);
			result.success("密码修改成功");
			return result;
		}
	}

	// 修改个人信息
	@RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public Result updateUserInfo(String name, String location, HttpSession session) {
		Result result = new Result();
		Client client = (Client) session.getAttribute("c");
		if (client == null) {
			result.fail("请登录");
			return result;
		}
		client.setcLocation(location);
		client.setcPersonName(name);
		clientService.updateClientInfo(client);
		result.success("修改成功");

		return result;
	}

	// 修改信息前，获取原本数据
	@RequestMapping(value = "/updateUserInfo", method = RequestMethod.GET)
	// @ResponseBody
	public String getUserInfo(HttpSession session, HttpServletRequest request) {
		Client client = (Client) session.getAttribute("c");
		request.setAttribute("c", client);

		return "personal";
	}

	// 跳转到查看订单页面
	@RequestMapping("/showOrder")
	public String showOrder(HttpServletRequest request) {
		int cId = ((Client) request.getSession().getAttribute("c")).getcId();
		List<Vorder> orders = orderService.showOrderByClientId(cId);
		List<Merchant> merchants = merchantService.findAllMerchant();
		if (orders != null) {
			for (Vorder order : orders) {
				System.out.println(order.getoId());
				for (Merchant merchant : merchants) {
					if (merchant.getmId().equals(order.getmId())) {
						order.setMerchant_name(merchant.getmBrand());
					}
				}
			}
		}
		request.getSession().setAttribute("orders", orders);
		return "order";
	}

}
